package com.wiscamp.ninechapters.problems.domain.models.types;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.wiscamp.ninechapters.problems.enums.CommonQuestionModes;
import com.wiscamp.ninechapters.problems.enums.ProblemModes;
import com.wiscamp.ninechapters.problems.enums.StemTypes;
import cube.ddd.domain.Entity;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 题类型
 */
@Data
public class ProblemType implements Entity {
    /**
     * 题型编号
     */
    private long typeId = 0;

    /**
     * 学科编号
     */
    private long subjectId = 0;

    /**
     * 题型代码
     */
    private String typeCode = "";

    /**
     * 题型名称
     */
    private String typeName = "";

    /**
     * 题型说明
     */
    private String typeDescription = "";

    /**
     * 题型说明语音
     */
    private String typeAudio = "";

    /**
     * 是否有题干部分
     */
    private boolean hasStem = false;

    /**
     * 题干标题
     */
    private String stemTitle = "";

    /**
     * 题干子标题
     */
    private String stemSubtitle = "";

    /**
     * 题干类型
     * 参考 panels.data_dictionary: STEM_TYPE
     */
    private StemTypes stemType;

    private int stemTypeId;

    /**
     * 题干语音播放次数
     */
    private int stemPlayTimes = 0;

    private int problemModeId;

    private int commonQuestionModeId;

    /**
     * 备注
     */
    private String remark = "";

    /**
     * 是否可用
     */
    private boolean enabled = true;

    /**
     * 是否删除
     */
    private boolean deleted = false;

    /**
     * 创建用户
     */
    private long creatorId = 0;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0;

    /**
     * 编辑时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime modificationTime;

    private String subjectName = "";

    private String stemTypeName = "";

    private String problemModeName = "";

    private String commonQuestionModeName = "";

    private String creatorName = "";

    private String modifierName = "";

    /**
     * 问题题型类型
     */
    private Collection<Long> questionTypeIds = new ArrayList<>();

    private Collection<QuestionType> questionTypes = new ArrayList<>();

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public ProblemModes getProblemMode() {
        return ProblemModes.valueOf(this.problemModeId);
    }

    public CommonQuestionModes getCommonQuestionMode() {
        return CommonQuestionModes.valueOf(this.commonQuestionModeId);
    }
}
